package com.nervenets.web.controller;

import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.request.SettlementAccountOpenRequestV1;
import com.icbc.api.response.SettlementAccountOpenResponseV1;
import com.icbc.util.GenerateKeyUtils;
import com.icbc.util.MD5Util;
import com.icbc.util.SecretUtils;
import com.nervenets.Global;
import com.nervenets.utils.JodaUtils;
import com.nervenets.utils.StringUtils;
import com.nervenets.web.hibernate.domain.AccountOpen;
import com.nervenets.web.hibernate.domain.AccountOpenBinding;
import com.nervenets.web.hibernate.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "测试接口v1", description = "测试类接口")
@RestController
@RequestMapping("/api/v1/test")
public class TestController extends BaseController {
    private static final String APP_ID = Global.getApplicationProperties().getProperty("app.id");
    private static final String MY_PRIVATE_KEY = Global.getApplicationProperties().getProperty("my.private.key");
    private static final String APIGW_PUBLIC_KEY = Global.getApplicationProperties().getProperty("apigw.public.key");
    private static final String MY_SM2_PRIVATE_KEY = Global.getApplicationProperties().getProperty("my.sm2.private.key");
    private static final String ICBC_SM2_PUBLIC_KEY = Global.getApplicationProperties().getProperty("icbc.sm2.public.key");
    private static final String MY_API_DOMAIN = Global.getApplicationProperties().getProperty("app.base.domain");

    @ApiOperation("管理员权限列表")
    @PostMapping("/adminRoles")
    public String adminRoles() {
        return successMessage(Global.allAdministratorRoles());
    }

    @ApiOperation("开户测试")
    @PostMapping("/openTest")
    public String openTest() throws Exception {
        AccountOpen open = new AccountOpen();
        open.setId(123457L);
        open.setCreateTime(1345555555);
        open.setUser(new User(StringUtils.getRandomString(32), StringUtils.getRandomString(32), "王琳"));
        open.setTrueName("王琳");
        open.setIdCard("330100199005301615");
        open.setIdCardInvalidDate("2037-05-17");
        open.setIdCardSignDate("2017-05-17");
        AccountOpenBinding binding = new AccountOpenBinding();
        binding.setId(123459L);
        binding.setOpen(open);
        binding.setMobile("18100000010");
        binding.setCorpSerNo(StringUtils.getRandomString(16));
        binding.setBindCard("6222803090000000004");

        String sm4Key = GenerateKeyUtils.generateSM4Key();  //生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(APP_ID, MY_PRIVATE_KEY, APIGW_PUBLIC_KEY);
        SettlementAccountOpenRequestV1 request = new SettlementAccountOpenRequestV1();
        SettlementAccountOpenRequestV1.SettlementAccountOpenRequestV1Biz bizContent = new SettlementAccountOpenRequestV1.SettlementAccountOpenRequestV1Biz();
        request.setServiceUrl(Global.getApplicationProperties().getProperty("icbc.url") + Global.getApplicationProperties().getProperty("icbc.SettlementAccountOpenV1")); //API地址
        bizContent.setCorpNo(APP_ID);//合作方机构编号
        bizContent.setTrxAccDate(JodaUtils.timeLongToString("yyyy-MM-dd")); //合作方交易日期
        bizContent.setTrxAccTime(JodaUtils.timeLongToString("HH:mm:ss"));//合作方交易时间
        bizContent.setCorpDate(JodaUtils.timeLongToString("yyyy-MM-dd")); //合作方工作日期
        bizContent.setCorpSerno(binding.getCorpSerNo()); //合作方交易单号，保存单号，短信验证/订单查询时使用
        bizContent.setOutServiceCode("openaccount"); //外部服务代码
        bizContent.setCorpMediumId(open.getUser().getOpenId());//合作方介质号，不能重复，介质不同对应可以开辟多个II类账户
        bizContent.setBindMedium(SecretUtils.encryptSM4(sm4Key, binding.getBindCard()));//sm4加密的一类户卡号
        bizContent.setCertType(0); //证件类型
        bizContent.setCertNo(SecretUtils.encryptSM4(sm4Key, open.getIdCard()));  //SM4加密的证件号码  使用ECB会报324异常
        bizContent.setCustName(SecretUtils.encryptSM4(sm4Key, open.getTrueName()));//SM4加密的客户姓名
        bizContent.setMobileNo(SecretUtils.encryptSM4(sm4Key, binding.getMobile()));//SM4加密的手机号码
        bizContent.setOccupation(1);//职业
        if ("forever".equals(open.getIdCardInvalidDate())) {
            bizContent.setValidityPeriod("2039-12-31");
        } else {
            bizContent.setValidityPeriod(open.getIdCardInvalidDate());//证件失效期
        }
        bizContent.setSignDate(open.getIdCardSignDate()); //证件签发日期
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key, ICBC_SM2_PUBLIC_KEY)); //使用SM2加密的SM4对称密钥
        bizContent.setBindMediumHash(MD5Util.string2MD5(binding.getBindCard())); //绑定的I类卡的HASH值
        bizContent.setNotifyAddr(String.format("https://%s/api/v1/bank/openNotify", MY_API_DOMAIN));
        request.setBizContent(bizContent);
        System.out.println(com.icbc.api.internal.util.internal.util.fastjson.JSON.toJSONString(request));
        SettlementAccountOpenResponseV1 response = client.execute(request);
        System.out.println(com.icbc.api.internal.util.internal.util.fastjson.JSON.toJSONString(response));
        if (response.isSuccess() && response.getReturnCode() == 0) {
            System.out.println(binding.getCorpSerNo());
        } else {
        }
        return successMessage(response);
    }
}
